import 'package:part_time_job_pro/common/index.dart';
import 'package:part_time_job_pro/common/models/login_user.dart';
import 'package:part_time_job_pro/common/utils/def_type.dart';
import 'package:part_time_job_pro/common/utils/request_util.dart';

/// 用户相关接口
class UserApi {
  late RequestUtil apiUtil;
  //创建实例
  UserApi() {
    apiUtil = RequestUtil.getInstance();
  }

  /*
   * 账号密码登录
   * @param account 登录账号
   * @param password 登录密码
   */
  accountLogin(
      RequestCallBack requestCallBack, Map<String, dynamic> params) async {
    try {
      var result = await apiUtil.post(HttpConfig.accountLogin,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultData = result[ResponesCode.dataTag];
        LoginUser user = LoginUser.fromJson(resultData);
        requestCallBack(user);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(e);
    }
  }

  /*
   * 验证码登录
   * @param mobile 手机号码
   * @param smscode 验证码
   */
  smsCodeLogin(
      RequestCallBack requestCallBack, Map<String, dynamic> params) async {
    try {
      var result = await apiUtil.post(HttpConfig.smsCodeLogin,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultData = result[ResponesCode.dataTag];
        LoginUser user = LoginUser.fromJson(resultData);
        requestCallBack(user);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(e);
    }
  }

  /*
   * 用户注册
   * @param 参数信息
   */
  registerAccount(
      RequestCallBack requestCallBack, Map<String, dynamic> params) async {
    try {
      var result = await apiUtil.post(HttpConfig.userRegister,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultData = result[ResponesCode.dataTag];
        LoginUser user = LoginUser.fromJson(resultData);
        requestCallBack(user);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(e);
    }
  }

  /*
   * 重置密码
   * @param 参数信息
   */
  resetAccPassword(
      RequestCallBack requestCallBack, Map<String, dynamic> params) async {
    try {
      var result = await apiUtil.post(HttpConfig.resetPassowrd,
          decode: true, data: params);
      requestCallBack(result);
    } catch (e) {
      requestCallBack(e);
    }
  }

  /*
   * 退出登录
   * @param 参数信息
   */
  loginOut(RequestCallBack requestCallBack) async {
    try {
      var result = await apiUtil.post(HttpConfig.userLogout, decode: true);
      requestCallBack(result);
    } catch (e) {
      requestCallBack(e);
    }
  }
}
